package cn.wolfcode.luowowo.website.web.advice;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ControllerAdvice 在请求映射方法处理前后, 可以对处理方法进行功能性增强
 * <p>
 * 处理前:比如时间类型参数的格式化
 * 处理后 : 统一异常处理
 */
@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object logicException(Exception e, HttpServletResponse resp) throws IOException {
        return new AjaxResult(false, e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object RuntimeException(Exception e, HttpServletResponse resp) throws IOException {
        return new AjaxResult(false, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object runTimeExp(Exception e, HttpServletResponse resp) throws IOException {
        return new AjaxResult(false, "系统出现未知异常，请联系客服。");
    }
}